package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.ScheduleBO;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.ScheduleDao;
import com.jackiechancinema.ssm.dto.OrderExcelDto;
import com.jackiechancinema.ssm.dto.ScheduleExcelDTO;
import com.jackiechancinema.ssm.entity.Commodit;
import com.jackiechancinema.ssm.entity.Order;
import com.jackiechancinema.ssm.entity.Schedule;
import com.jackiechancinema.ssm.listener.ScheduleReadListener;
import com.jackiechancinema.ssm.service.Inte.ScheduleService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleDao scheduleDao;
    @Autowired
    private ScheduleReadListener listener;

    @Override
    public PageVo<Schedule> page(int page, int limit) {
        PageHelper.startPage(page, limit);
        //2.获取分页的查询结果
        PageInfo<Schedule> pageInfo=new PageInfo<>(scheduleDao.queryAll());
        return new PageVo<Schedule>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R delete(int scheduleId) {
        int num = scheduleDao.delete(scheduleId);
        if(num>0){
            return R.ok("删除成功");
        }else{
            return R.fail();
        }
    }


    @Override
    public R add(Schedule schedule) {
        schedule.setScheduleBegin(new Date());
        int num = scheduleDao.insert(schedule);
        if(num>0){
            return R.ok("添加成功");
        }else{
            return R.fail();
        }
    }

    @Override
    public R update(Schedule schedule) {
        int num = scheduleDao.update(schedule);
        if(num>0){
            return R.ok("修改成功");
        }else{
            return R.fail();
        }
    }

    @Override
    public PageVo<Schedule> page(ScheduleBO bo) {
        if(bo!=null){
            //1.组装查询条件
            Schedule sc=new Schedule();
            if(StringUtils.hasLength(bo.getMoviceName())){
                sc.setMoviceName(bo.getMoviceName());
            }
            if(StringUtils.hasLength(bo.getHallName())){
                sc.setHallName(bo.getHallName());
            }
            //2.开启分页插件
            PageHelper.startPage(bo.getPage(), bo.getLimit());
            //3.获取分页的查询结果
            PageInfo<Schedule> pageInfo=new PageInfo<>(scheduleDao.queryAllPlus(sc));
            //4.返回结果
            return new PageVo<Schedule>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Schedule>(SystemContant.PAGE_R,"OK",0,null);
    }

    @Override
    public R batchadd(MultipartFile file) {
        if(!file.isEmpty()){
            //excel解析
            try {
                EasyExcel.read(file.getInputStream(), ScheduleExcelDTO.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(ScheduleBO bo, HttpServletResponse response) {
        String fn = System.currentTimeMillis()+".xlsx";


        response.setHeader("content-disposition","attachment;filename="+fn);

        //1.组装查询条件
        Schedule sc=new Schedule();
        if(StringUtils.hasLength(bo.getMoviceName())){
            sc.setMoviceName(bo.getMoviceName());
        }
        if(StringUtils.hasLength(bo.getHallName())){
            sc.setHallName(bo.getHallName());
        }
        //3.获取要下载的数据
        List<Schedule> list=scheduleDao.queryAllPlus(sc);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Schedule.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
